/**
 * 用户信息补全
 *
 */
module.exports = function ($state, $interval,$rootScope, registerSuccessService, settingService, httpservice,$scope) {
  $rootScope.loadingShow = false;

  var vm = this;


  //默认用户类型为律师 性别为男
  vm.userInfo = {userType: 1, sex: 0};

  //获取律所信息
  vm.lawfirmList = [];
  vm.getLawfirmList = getLawfirmList;
  //发送验证码
  vm.sendCode = sendCode;
  vm.saveUserInfo = saveUserInfo;
  vm.getUserInfo = getUserInfo;
  vm.checkCellPhoneExistUrl = httpservice.checkCellPhoneExistUrl;
  vm.lawfirmSelected = null;//选中的律所
  vm.sendMessage = {
    disable: false,
    text: '发送验证码'
  };
  init();
  function init() {
    getUserInfo();
    $('input').on('keydown',function(e){
      var e = e || event;
      var currKey = e.keyCode || e.which || e.charCode;//支持IE,FireFox
      if (currKey == 13) {
        return false;
      }
    })
  }

  /**
   * 日期选择器
   */
  $(".form_datetime").datetimepicker({
    minView: 3,//设置最小视图为年视图
    format: 'yyyy-mm',
    language: "zh-CN",
    charset: "utf-8",
    startView: 3, //这里就设置了默认视图为年视图
    maxView:'decade',
    endDate:new Date()
  });

  /**
   * 更改日期事件
   */
  $('.form_datetime')
    .datetimepicker()
    .on('changeDate', function (ev) {
      $('.form_datetime').datetimepicker('hide');
    });

  function getUserInfo() {
    settingService.queryUserInfo().then(function (data) {
      if (data.code === 1) {
        vm.userInfo.createdate = data.data.createdate;
        vm.lawfirmSelected = {
          id: data.data.lawfirmid,
          name: data.data.lawfirmname
        }
      }
    })
  }



  function getLawfirmList(name) {

    return registerSuccessService.getLawfirm(name).then(function (response) {
      if (response.data.code == 1) {
        return response.data.data;
      }
    })
  }

  function sendCode() {
    if (vm.userInfo.cellphone) {
      vm.sendMessage.disable = true;
      //发送
      registerSuccessService.sendCode(vm.userInfo.cellphone).then(function (response) {
        if (response.data.code == 1) {
          //发送成功，开始倒计时
          var times = 60;
          vm.sendBtnDisable = true;
          var coutdown = $interval(function () {
            if (times > 1) {
              times--;
              vm.sendMessage.disable = true;
              vm.sendMessage.text = " 重新发送 (" + times + "S) ";
            } else {
              $interval.cancel(coutdown);
              vm.sendMessage.disable = false;
              vm.sendMessage.text = " 重新发送 ";
            }
          }, 1000);
        } else {
          vm.sendMessage.disable = false;
        }
      })
    } else {
      vm.sendMessage.disable = false;
    }
  }

  function saveUserInfo() {
    //检查律所是否选择
    if (vm.lawfirmSelected && vm.lawfirmSelected.id) {
      vm.userInfo.lawfirmid = vm.lawfirmSelected.id;
      $('.lvs .errorR').hide();
    } else {
     $('.lvs .errorR').show();
      return;
    }
    registerSuccessService.updateUserInfo(vm.userInfo).then(function (response) {
      if (response.data.code == 1) {
        $scope.headImageUrl = response.data.user.iconUrl;
        $state.go('workBarNoWork');//跳转到工作台
      }
    })
  }






};
